﻿// JavaScript Document 一
var leftMuTxt = null;
var subMuTxt = null;
var subSubMuTxt = null;
/* comment by dj
function bodyOnLoad(){	
	synLayoutWidth();
	try{
		checkValidBrowserType();
	}catch(e){
	}
	disableAutoComplete();
	buttonsInit();	 
}
*/


function buttonsInit(){
	var _btns = CE.getElementsByClassName(document.body, 'input', 'inputButton', true);
	for(var x=0; x<_btns.length; x++){
		if(_btns[x].disabled == true){
			//_btns[i].disabled = false;
			//_btns[i].onclick = function(){alert(this.tagName);}
			disableButton(_btns[x]);
		}
	}
}
function disableAllButtons(){
	allButtons(true);
}
function enableAllButtons(){
	allButtons(false);
}
function allButtons(_disable){
	var _btns = CE.getElementsByClassName(document.body, 'input', 'inputButton', true);
	for(var x=0; x<_btns.length; x++){
		if(_disable)
			disableButton(_btns[x]);
		else
			enableButton(_btns[x]);
	}		
}
function disableButton(_btn){
	var _bnDiv = CE.getParentByClassName(_btn, 'button', false);
	if(_bnDiv){
		//_bnDiv.style.border = '1px solid red';
		if(_bnDiv.className.indexOf('sbtn') != -1){
			_bnDiv.className = 'button sbtn sbtnDisabled';
		}else{
			_bnDiv.className = 'button btnDisabled';
		}
		_bnDiv.style.cursor = 'default';						
	}
	_btn.disabled = true;
	_btn.style.cursor = 'default';	
}
function enableButton(_btn){
	var _bnDiv = CE.getParentByClassName(_btn, 'button', false);
	if(_bnDiv){
		//_bnDiv.style.border = '1px solid red';
		if(_bnDiv.className.indexOf('sbtn') != -1){
			_bnDiv.className = 'button sbtn';
		}else{
			_bnDiv.className = 'button';
		} 
		_bnDiv.style.cursor = 'pointer';								
	} 		
	_btn.disabled = false;
	_btn.style.cursor = 'pointer';	
}

function backAction(){
	window.history.back();	
}


function isValidBrowser(){
	var flag = false
	var _ub = window.navigator.userAgent.toUpperCase();
	if(_ub.indexOf("FIREFOX/3") != -1||_ub.indexOf("FIREFOX/4") != -1){
		temp=true;
	}else{
		temp=false;
	}
	if(CE.browser('IE7') || CE.browser('IE8') || temp){
		flag = true;	
	}	
	return flag;
}

 
function disableAutoComplete(){
	var forms = document.forms;
	if(forms.length == 0)
		return;
	for(var i=0; i<forms.length; i++){
		//forms[i].style.border = '1px solid red';		
		forms[i].setAttribute("autocomplete", "off"); 		
		var elmts = null;		
		elmts = forms[i].elements; 
		for(var j=0; j<elmts.length; j++){
			if(elmts[j].tagName.toLowerCase() == 'input'){
				if(elmts[j].type.toLowerCase() == 'text' ||
					elmts[j].type.toLowerCase() == 'password'){
					//elmts[j].style.border = '1px solid red';
					elmts[j].setAttribute("autocomplete", "off"); 
				}
			}
		}
	}	
}

function changeLang(lang){
	if(lang == 'tc' || lang == 'sc'){
		if(confirm('Your screen data may be lost.')){
			window.location = 'home.html';
		}		
	}
}

/* onclick="selectPage(event, this, 'selectEmailTemplate')" */
function selectPage(e, thisObj, _type){
	if(_type == 'coordinator'){
		popup(e, '../common/select_coordinator_search.html', 'width:700;height:500');
	}	
	if(_type == 'supervisor'){
		popup(e, '../common/select_supervisor_search.html', 'width:700;height:500');
	}
	if(_type == 'teacher'){
		popup(e, '../common/select_teacher_search.html', 'width:700;height:500');
	}	 
	if(_type == 'marker_group'){
		popup(e, '../common/select_marker_group_search.html', 'width:700;height:500');
	}
}

function hideIfSelected(obj, valueType, value, targetId){
	var _selected = CE.selected(obj, valueType);
	var _target = CE.$(targetId);
	if(_selected == value){
		_target.style.display ="none";
	}else {
		_target.style.display ="";
	}
}

function showIfSelected(obj, valueType, value, targetId){
	var _selected = CE.selected(obj, valueType);
	var _target = CE.$(targetId);
	if(_selected == value){
		_target.style.display = "";
	}else {
		_target.style.display = "none";
	}
}

function hideOrShow(targetId){
	var _target = CE.$(targetId);
	if (_target.style.display == "none"){
		_target.style.display ="";
	} else {
		_target.style.display = "none";
	}
}
function hide(targetId){
	var _target = CE.$(targetId);
	_target.style.display = "none";
}
function show(targetId){
	var _target = CE.$(targetId);
	_target.style.display = "";
}

function changeRole(module){
	if(confirm("Please save all data before changing role?")){
		window.location = "../"+module+"/select_role.html" ;
	}
}


function showIfChecked(obj, targetId){
	var _target = CE.$(targetId);
	if(obj.checked == true){
		_target.style.display = "";
	}else {
		_target.style.display = "none";
	}
}
function hideIfChecked(obj, targetId){
	var _target = CE.$(targetId);
	if(obj.checked == false){
		_target.style.display = "";
	}else {
		_target.style.display = "none";
	}
} 


function actionOnLogo(){
	window.location = "../index.html";	
}

 
function changeFont(_pNum){
	STYLE.changeFontSize(_pNum);
}

//------------------------------------------------------------------
//--Function: When click the tab, change the background color
//        
//--Author: LiuJian
//--Date: 2011-7-21
//------------------------------------------------------------------

function selectTab(menuId, tabId){
	var _tab = CE.$(menuId);
	var cells = _tab.rows[0].cells;	
	for(var i=0; i<cells.length; i++){
		if(cells[i].id == tabId){
			cells[i].className = "item2";
		}else{
			cells[i].className = "item1";
		}
	}
}

function selectGetOptGroupOnChange(_thisObj, tgtName){
	var _selectedOpt = 	_thisObj.options[_thisObj.selectedIndex];
	var _parentOptGroup = CE.getParentByTagName(_selectedOpt, "optgroup");
	if(_parentOptGroup.label != "" && _parentOptGroup.label != undefined){ 
		CE.$(tgtName).innerHTML = _parentOptGroup.label;
	}
}


function collapse(thisObj){
	var _tr = thisObj;
	var _table = CE.getParentByTagName(thisObj, "table");
	var _c = _tr.rowIndex;
	var _t = _table.rows.length;	
	for(var i=_c+1; i<_t; i++){
		var _cTr = _table.rows[i];
		if(_tr.className == _cTr.className){
			break;
		}else{
			if(_cTr.style.display == ""){
				_cTr.style.display = "none";
			}else{
				_cTr.style.display = "";
			}
		}
	}
}




















//function resetScrollDivWidth(divLeft, divRight){
//	var _divLeft = CE.$(divLeft);
//	var _divRight = CE.$(divRight);
//	_divLeft.style.border = "1px solid red";
//	CE.pty(_divLeft);
//	alert(_divLeft.W);
//}





function synLayoutWidth(){
	var _lftMenuOffset = 250;  
	var bdrOffset = 38;  	
	var _lftMu = CE.getElementsByClassName(document, "div", "lftMu", true);
	if(_lftMu == "")
		_lftMenuOffset = 0;
	var _bodys = CE.getElementsByClassName(document, "div", "body", true);
	var _body = _bodys[0];
	var _bodyW = CE.pty(_bodys[0]).W;
	var _sLists = CE.getElementsByClassName(document, "div", "sList", true);
	var _maxWidth = 0;
	for(var i=0; i<_sLists.length; i++){		
		var _tW = CE.pty(_sLists[i]).W ;
		if(_tW > _maxWidth){
			_maxWidth = _tW;
		}
	}
	var _sForm1s = CE.getElementsByClassName(document, "div", "form1", true); 
	for(var i=0; i<_sForm1s.length; i++){	
		var _tW = CE.pty(_sForm1s[i]).W;
		if(_tW > _maxWidth){
			_maxWidth = _tW;
		}
	}
	var _sForm2s = CE.getElementsByClassName(document, "div", "form2", true); 
	for(var i=0; i<_sForm2s.length; i++){	
		var _tW = CE.pty(_sForm2s[i]).W;
		if(_tW > _maxWidth){
			_maxWidth = _tW;
		}
	}
	if((_maxWidth + _lftMenuOffset) > _bodyW){		
		_body.style.width = _maxWidth + _lftMenuOffset + bdrOffset +"px";	
		//_body.style.border = "10px solid red";
		//_body.style.width = "1503px";
	}else{
		_body.style.width = ((CE.pty(_bodys[0]).W < 1003 ) ? 1003 : _bodyW) + "px";
	}
}







function popImages(e){
	var _s_form = CE.createElement("form");
	_s_form.target = "_blank";
	document.body.appendChild(_s_form);
	_s_form.action = "../common/pop_images.html";
	_s_form.submit();
	//popup(e, '../common/pop_images.html', 'width:700;height:500');
}
function popPdfs(e){
	//popup(e, '../common/select_school_admin_search.html', 'width:700;height:500');
}
function popDocs(e){
	//popup(e, '../common/select_school_admin_search.html', 'width:700;height:500');
}





function expandNextRow(thisObj){
		
}






function noOfCbxChecked(ctr){
	var ctrObj = CE.$(ctr);
	var cbxs = CE.getElementsByClassName(ctrObj, "input", "inputCheckbox", true);
	var _c = 0;
	for(var i=0; i<cbxs.length; i++){
		if(cbxs[i].checked){
			_c++;
		}
	}
	return _c;
}




function lftWqdSelected(_id){
	var lftWqdDiv = CE.$("lftWqdDiv");
	var ul = lftWqdDiv.getElementsByTagName("ul")[0];
	//ul.style.border = "1px solid red";
	var LIs = CE.LIs(ul);
	var _c = LIs.length;
	for(var i=0; i<LIs.length; i++){
		if(LIs[i].id == _id){
			LIs[i].className = "selected";
			if(i < LIs.length){
				LIs[i+1].style.backgroundColor = "white";	
			}
			_c = i;
		}		
		var _a = LIs[i].getElementsByTagName("a")[0];
		if(i > _c){
			//_a.disabled = true;
//			_a.href = "javascript:void(0)";				
//			_a.className = "lftWqdDivDisabled";
		}
	}
}


function lftWqdDisable(_id, _flag){
	var lftWqdDiv = CE.$("lftWqdDiv");
	var ul = lftWqdDiv.getElementsByTagName("ul")[0];
	var LIs = CE.LIs(ul);
	var _c = LIs.length;
	for(var i=0; i<LIs.length; i++){
		if(LIs[i].id == _id){
			var _a = LIs[i].getElementsByTagName("a")[0];
//			_a.href = "javascript:void(0)";				
//			_a.className = "lftWqdDivDisabled";
			if(_flag){
				_a.style.display = "none";
				LIs[i+1].style.backgroundColor = "white";	
			}else{
				_a.style.display = "";
				LIs[i+1].style.backgroundColor = "transparent";
			}
		}		
	}
}

function validateHKID(hkid) {

	if (hkid == "") {
		return false;
	}
	hkid = hkid.toUpperCase();
	var idcard_array = hkid.split("");

	if (idcard_array.length != 10) {
		return false;
	}
   
	if (!(idcard_array[0] >= 'A' && idcard_array[0] <= 'Z')) {
		return false;
	}

	var first = idcard_array[0];

	var total = ((first.charCodeAt(first) - 64) % 11) * 8;

	for ( var i = 7; i > 1; i--) {
		var j = parseInt(idcard_array[8 - i]) * i;
		total = total + j;
	}
	var left;
	if((total % 11)==0){
		left=0;
	}else{
	    left = 11 - (total % 11);
	}
	if (idcard_array[8] != 'A'&&left == parseInt(idcard_array[8])) {
		return true;
	} else if(idcard_array[8] == 'A'&&left == 10){
		return true;
	}else {
		return false;
	}

}
function validateDate(value){
	var isDate = true;
	if(value!=null){
		 value = value.replace(/(^\s*)|(\s*$)/g, "");
		 if(value!=""){
	       var detectedDateFormat = DE.getDateFormat(value);
	       if(detectedDateFormat == null){
	  	       isDate = false;
	       }
	      if(detectedDateFormat!="dd,mm,yyyy,-"){
		       isDate = false;
	      }
	      var dateSplit = value.split("-");
	      var year=dateSplit[2];
	      var month=dateSplit[1];
	      var day=dateSplit[0];
	      if (!(((year % 400)==0)||((year % 100!=0)&&(year % 4 ==0 ))) && (month==2) && (day>=29)){
		       isDate = false;
	      }
	      if ((month==2) && (day>=30)){
		       isDate = false;
	      }
	      if ((month<=7) && ((month % 2)==0) && (day>=31)){
		       isDate = false;
	      }
	      if ((month>=8) && ((month % 2)==1) && (day>=31)){
		       isDate = false;
	      }
	   }
	}
	return isDate;
}

function clearFilter(){
	var searchObjs=document.getElementsByTagName("input");
	if(searchObjs.length>0){
		for(var i=0;i<searchObjs.length;i++){
			if(searchObjs[i].type=="text"){
				searchObjs[i].value="";
			}
			if(searchObjs[i].type=="checkbox"){
				searchObjs[i].checked=false;
			}
		}
	}
	var selectObj=document.getElementsByTagName("select");
	if(selectObj.length>0){
		for(var j=0;j<selectObj.length;j++){
			if(selectObj[j].multiple){
				selectObj[j].selectedIndex=-1
			}else{
				selectObj[j].selectedIndex =0;
			}
		}
	}
	var labels =  document.getElementsByTagName("label");
	if(labels.length>0){
	   for(var i=0;i<labels.length;i++){  
		  if(labels[i].className=="error") {
			  labels[i].style.display="none";
		  } 
	   } 
	}
}

function getText(source, params) {
	$.each(params, function(i, n) {
		source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
	});
	return source;
}


//------------------------------------------------------------------
//--Function: group checkboxs, one checkbox can control others,
//          select all or select none.
//          Main control checkbox is parent, and orthers is children.
//
//--Author: LiuJian
//--Date: 2011-6-24
//------------------------------------------------------------------
function clickChild(parentId,childrenName){
    
    var parentCanChecked = true;
    
    var children =document.getElementsByName(childrenName);
    for(var i=0;i<children.length;i++){
        
        if(children[i].checked==false){
            parentCanChecked=false;
            break;
        }
    }
    
    document.getElementById(parentId).checked=parentCanChecked;
}

function clickParent(parentId,childrenName) {
  
  var parentChecked = document.getElementById(parentId).checked;
  var children = document.getElementsByName(childrenName);
  
  for (i = 0; i < children.length; i++) {
      children[i].checked = parentChecked;
  }
}


//------------------------------------------------------------------
//--Function: The height of the iframe can resize as the height of the content of itself.
// Parameters: thisDoc =self, iframeId=the id of iframe
//
//--Author: LiuJian
//--Date: 2011-6-24
//------------------------------------------------------------------
function iframeResize(thisDoc,iframeId,thisContainor){

  var iframeObj = thisDoc.parent.document.getElementById(iframeId);
  
  if(iframeObj != null) {
      var heightValue = 0;
      
      if(thisContainor!=null && thisContainor!=""){
          
          var containor = document.getElementById(thisContainor);
          
          heightValue = containor.scrollHeight+100;
          
      }else{
          
           heightValue = thisDoc.document.body.scrollHeight;
      }
      
      iframeObj.style.height = heightValue + "px";
  }
  
}


//change Unicode encoding to  Chinese  added by Truman  user by alert
function runicode(s){
	if(s.indexOf("&#")==-1){
		return s;
	}
	var k=s.split(";");
	var rs="";
	for(i=0;i<k.length;i++){
	var m=k[i].replace(/&#/,"");
	rs+=String.fromCharCode(m) ;
	}
	return rs;
}

// JavaScript Document